/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bean.business;

import entities.Student;
import java.util.LinkedList;
import java.util.List;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

/**
 *
 * @author HoangHai
 */
@Stateless
@LocalBean
public class studentBean {
    @PersistenceContext(unitName = "DeclarationBoard-ejbPU")
    private EntityManager em;

    public void persist(Object object) {
        em.persist(object);
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
    public List<Student> allStudent(){
        List<Student> students = em.createNamedQuery("Student.findAll").getResultList();
        return students;
    }
    
    public Student findStudentByID(String s){
        Student students = (Student) em.createNamedQuery("Student.findByStuID").setParameter("stuID", s).getSingleResult();
        return students;
    }
    
    public List<Student> findStudentByClassID(int id){
        List<Student> list = new LinkedList<Student>();
        list = em.createNamedQuery("Student.findByClassID").setParameter("classID", id).getResultList();
        return list;
    }
    
    public boolean addStudent(Student s){
        boolean flag;
        try {
            em.persist(s);
            flag = true;
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
    
    public boolean updateStudent(Student s){
        boolean flag;
        try {
            em.merge(s);
            flag = true;
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
    
    public boolean delStudent(String id){
        boolean flag;
        try {
            Student s = findStudentByID(id);
            em.remove(s);
            flag = true;
        } catch (Exception e) {
            flag = false;
        }
        return flag;
    }
}
